namespace one {
    export class DynamicFrames extends egret.DisplayObjectContainer {
        private mcData: { [label: string]: string[] | number[] };
        private fileName: string;

        private bitmap: egret.Bitmap;

        private event: egret.Event;
        public constructor(mcData: { [label: string]: string[] | number[] }, fileName: string, private ext: string = "png") {
            super();

            this.mcData = mcData;
            this.fileName = fileName;

            this.bitmap = new egret.Bitmap();
            this.addChild(this.bitmap);

            this.frameIntervalTime = 1000 / this._frameRate;

            this.addEventListener(egret.Event.ADDED_TO_STAGE, (e: egret.Event) => {
                if (e.currentTarget == e.target) {
                    egret.startTick(this.advanceTime, this);
                }
            }, this);
            this.addEventListener(egret.Event.REMOVED_FROM_STAGE, (e: egret.Event) => {
                if (e.currentTarget == e.target) {
                    egret.stopTick(this.advanceTime, this);
                }
            }, this);

            this.event = new egret.Event(egret.Event.COMPLETE);
            this.load();
        }

        private isLoaded:boolean = false;
        private load():void {
            this.isLoaded = false;
            let total:number = 0;
            let cnt:number = 0;
            let loadOver = ()=> {
                cnt++;

                if (cnt == total) {
                   this.isLoaded = true;
                }
            }

            for (let key in this.mcData) {
                for (let i = 0; i < this.mcData[key].length; i++) {
                    total++;
                }
            }
            for (let key in this.mcData) {
                for (let i = 0; i < this.mcData[key].length; i++) {
                    RES.getResAsync(this.fileName + "_" + this.mcData[key][i] + "_" + this.ext, loadOver, this);
                }
            }

        }

        private _frameRate: number = 24;
        public get frameRate(): number {
            return this._frameRate;
        }
        public set frameRate(v: number) {
            this._frameRate = v;
            this.frameIntervalTime = 1000 / this._frameRate;
        }

        private animation: string;
        private playTimes: number;
        private countTimes: number;
        public play(animation: string, playTimes: number = 0): void {
            this.animation = animation;
            this.playTimes = playTimes;
            this.countTimes = 0;
            this.lastFrame = -1;
            this.startTime = egret.getTimer();

            if (this.mcData[animation] == null && DEBUG) {
                console.log(animation + "   dddddd");
            }
            this.$totalFrames = this.mcData[animation].length;
        }

        private frameIntervalTime: number;

        /**
         * @private
         */
        private startTime: number = 0;
        /**
         * @private
         */
        $totalFrames: number = 0;
        private advanceTime(timeStamp: number): boolean {
            if (!this.isLoaded) {
                return false;
            }
            if (this.playTimes > 0 && this.countTimes >= this.playTimes) {
                return false;
            }

            let self = this;
            let frameIntervalTime: number = self.frameIntervalTime;

            let t = egret.getTimer();

            let currentTime = t - this.startTime;
            if (currentTime < 0) {
                return false;
            }
            let tFrames = Math.floor(currentTime / frameIntervalTime);
            if (this.playTimes > 0 && tFrames >= (this.countTimes + 1) * this.$totalFrames) {
                this.countTimes = Math.floor(tFrames / this.$totalFrames);

                this.countTimes = Math.min(this.countTimes, this.playTimes);
            }

            let num: number = 0;
            if (this.playTimes > 0 && this.countTimes >= this.playTimes) {
                num = this.$totalFrames - 1;
                this.showFrame(num);
                this.dispatchEvent(this.event);
            }
            else {
                num = tFrames % this.$totalFrames;
                // if(num < 0) {
                //     num += this.$totalFrames;
                // }
                this.showFrame(num);
            }

            return false;
        }

        private lastFrame = -1;

        private showFrame(frame: number): void {
            if (this.lastFrame == frame) {
                return;
            }
            this.lastFrame = frame;

            let texture = RES.getRes(this.fileName + "_" + this.mcData[this.animation][frame] + "_" + this.ext) as egret.Texture;

            if (DEBUG) {
                if (!texture) {
                    console.warn(this.fileName + "_" + this.animation + "_" + frame)
                }
            }

            this.bitmap.texture = texture;

            this.bitmap.anchorOffsetX = texture.textureWidth * this.horizontalCenter / 100;
            this.bitmap.anchorOffsetY = texture.textureHeight * this.verticalCenter / 100;
        }

        horizontalCenter: number = 0;//0 左对齐  50 居中  100 右对齐
        verticalCenter: number = 0;
    }
}